package com.yz.android_baozou.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.yz.android_baozou.R;
import com.yz.android_baozou.adapter.Column_RecyclerAdapter;
import com.yz.android_baozou.bean.ColumnBean;
import com.yz.android_baozou.decoration.SpacesItemDecoration;
import com.yz.android_baozou.helper.OkhttpHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/11/2.
 */

public class ColumnFragment extends Fragment {
    @Bind(R.id.recyclerView_Column_main)
    RecyclerView recyclerViewColumnMain;
    @Bind(R.id.scoreView_Column)
    PullToRefreshScrollView scoreViewColumn;
    private Context mContext;
    private Column_RecyclerAdapter recyclerAdapter;
    //声明频道的实体类
    private ColumnBean columnBean;
    public String PATH_COLUMN = "http://dailyapi.ibaozou.com/api/v31/channels/index?page=1&per_page=10&";
    //声明频道的实体类下的集合
    private List<ColumnBean.DataBean> mList_ColumnBean = new ArrayList<>();
    //根据flag的值判断上拉刷新还是下拉加载更多
    private boolean flag;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_column_main, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadDataFromNet();
        scoreListener();
    }

    //根据pulltoRefresh的进行上拉或者下拉刷新
    private void scoreListener() {
        scoreViewColumn.setMode(PullToRefreshBase.Mode.BOTH);
        //滚动监听
        scoreViewColumn.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            //下拉刷新
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                flag = true;
                PATH_COLUMN = "http://dailyapi.ibaozou.com/api/v31/channels/index?page=1&per_page=10&";
                loadDataFromNet();
            }

            @Override
            //上拉加载更多 获取新的网址
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                if (PATH_COLUMN.equals("http://dailyapi.ibaozou.com/api/v31/channels/index?page=1&per_page=10&")) {
                    flag = false;
                    PATH_COLUMN = "http://dailyapi.ibaozou.com/api/v31/channels/index?page=2&per_page=10&";
                    loadDataFromNet();
                } else {
                    Toast.makeText(mContext, "别拉了，已经是最后一页了", Toast.LENGTH_SHORT).show();
                    scoreViewColumn.onRefreshComplete();
                }
            }
        });
    }

    private void loadDataFromNet() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String json = OkhttpHelper.getStringFromUrl(PATH_COLUMN);
                columnBean = parseJsonToBean(json);
                if (columnBean != null) {
                    mList_ColumnBean = columnBean.getData();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (flag) {
                                recyclerAdapter.reloadRecyclerView(mList_ColumnBean, true);
                            } else {
                                recyclerAdapter.reloadRecyclerView(mList_ColumnBean, false);
                            }
                            scoreViewColumn.onRefreshComplete();
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerViewColumnMain.addItemDecoration(new SpacesItemDecoration(10));
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerViewColumnMain.setLayoutManager(manager);
        recyclerViewColumnMain.setHasFixedSize(true);
        recyclerAdapter = new Column_RecyclerAdapter(mContext, mList_ColumnBean, recyclerViewColumnMain);
        recyclerViewColumnMain.setAdapter(recyclerAdapter);
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_column, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_refresh_column:
                break;
            case R.id.action_tonight_column:
                break;
            case R.id.action_settings_column:
                break;
        }
        Toast.makeText(mContext, "敬请期待", Toast.LENGTH_SHORT).show();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public ColumnBean parseJsonToBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<ColumnBean>() {
        }.getType());
    }
}
